package com.jh.concurrent.juc.countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 * @author JH_Wong
 * @projectName newforward
 * @title CountDownLatchDemo.java
 * @package com.jh.concurrent.juc.countdownlatch
 * @description CountDownLatch计数器例子
 * @date 2019-05-20 17:46
 */
public class CountDownLatchDemo extends Thread{

    public final static CountDownLatch latch = new CountDownLatch(1);

    @Override
    public void run() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-执行完毕");
    }

    public static void main(String[] args) {
        //base();

        for (int i = 0; i < 1000; i++) {
            new Thread(new CountDownLatchDemo()).start();
        }

        latch.countDown();
    }

    private static void demo() {
        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "-执行中");
            countDownLatch.countDown();
            System.out.println(Thread.currentThread().getName() + "-执行完成");
        }, "Thread1").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "-执行中");
            countDownLatch.countDown();
            System.out.println(Thread.currentThread().getName() + "-执行完成");
        }, "Thread2").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "-执行中");
            countDownLatch.countDown();
            System.out.println(Thread.currentThread().getName() + "-执行完成");
        }, "Thread3").start();

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("所有都执行完了");
    }

}
